/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var effectiveFPMS = 60 / 1000;


function Cube(cubeNumber){
    this.cubeNumber = cubeNumber;
    var sizeFaktor = 10/dimension;
    this.posX = (Math.floor(cubeNumber/dimension)-Math.floor(dimension/2.0))*sizeFaktor;
    this.posY = 0;
    this.posZ = (cubeNumber%dimension - Math.floor(dimension/2))*sizeFaktor;

    this.angle = 90;
    this.scale = 1.0;
   
 //   var densitycolor = dimension/cubeNumber;
    // Set the colors to a starting value.
    this.r = 0.5;
    this.g = 0.5;
    this.b = 0.5;
    
}
Cube.prototype.draw = function(tilt, spin) {
    mvPushMatrix();
    
    mat4.rotate(mvMatrix, degToRad(this.angle), [0.0, 0.0, 0.0]);
    mat4.translate(mvMatrix, [this.posX, this.posY, this.posZ]);
    
    mat4.rotate(mvMatrix, degToRad(-this.angle), [1.0, 0.0, 0.0]);
    mat4.rotate(mvMatrix, degToRad(-tilt), [0.0, 0.0, 0.0]);

    // All cubess spin around the Z axis at the same rate
    mat4.rotate(mvMatrix, degToRad(spin), [0.0, 0.0, 0.0]);

    //var vector = vec3.create([0.0,0.0,0.0]);
    //vector = vec3.set(vector, dest)
    mat4.scale(mvMatrix, [1.0, 1.0, this.scale]);
document.getElementById("warning").innerHTML=this.scale;
    // Draw the cubes in its main color
    gl.uniform3f(shaderProgram.colorUniform, this.r, this.g, this.b);
    drawCube(this.cubeNumber);
    mvPopMatrix();
};



Cube.prototype.animate = function(elapsedTime){
  /*  //this.angle += this.rotationSpeed * effectiveFPMS * elapsedTime;
    // Decrease the distance, resetting the star to the outside of
    // the spiral if it's at the center.
   // this.dist -= 0.01 * effectiveFPMS * elapsedTime;
    if (this.dist < 0.0) {
      this.dist += 5.0;
      this.randomiseColors();
    }*/
};
  
Cube.prototype.cubeColor = function(densityColor) {
    // Give the cube a color to symbolize the value it represent
    var i = densityColor/arrayMax;
    if (i>=0.5) {
        this.r = (i-0.5)*2;
    } else {
        this.g = i*2;
    }
    if (i<0.75 && i>0.25){
        this.b = (i-0.25)*2;
    }
    // this.b = Math.random();
};
Cube.prototype.cubeScale = function(scaleFaktor){
    this.scale = (scaleFaktor/arrayMax)*10 +0.1;
   // this.scale = 20;
};

function initCubeBuffers() {
    var faceSize = 10/dimension/2;
    var cubeHeight = 0.2
    cubeVertexPositionBuffer = gl.createBuffer();
    gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexPositionBuffer);
    vertices = [
        // Front face
      -faceSize, -faceSize,  cubeHeight,
       faceSize, -faceSize,  cubeHeight,
       faceSize,  faceSize,  cubeHeight,
      -faceSize,  faceSize,  cubeHeight,

      // Back face
      -faceSize, -faceSize, 0,
      -faceSize,  faceSize, 0,
       faceSize,  faceSize, 0,
       faceSize, -faceSize, 0,

      // Top face
      -faceSize,  faceSize, 0,
      -faceSize,  faceSize,  cubeHeight,
       faceSize,  faceSize,  cubeHeight,
       faceSize,  faceSize, 0,

      // Bottom face
      -faceSize, -faceSize, 0,
       faceSize, -faceSize, 0,
       faceSize, -faceSize,  cubeHeight,
      -faceSize, -faceSize,  cubeHeight,

      // Right face
       faceSize, -faceSize, 0,
       faceSize,  faceSize, 0,
       faceSize,  faceSize,  cubeHeight,
       faceSize, -faceSize,  cubeHeight,

      // Left face
      -faceSize, -faceSize, 0,
      -faceSize, -faceSize,  cubeHeight,
      -faceSize,  faceSize,  cubeHeight,
      -faceSize,  faceSize, 0,
    ];
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.DYNAMIC_DRAW);
    cubeVertexPositionBuffer.itemSize = 3;
    cubeVertexPositionBuffer.numItems = 24;

    var texturePartition = 1.0/dimension;
    var cubeVertexTextureCoordBuffer;
    
    for(var i = 0; i<dimension*dimension; i++){
        cubeVertexTextureCoordBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, cubeVertexTextureCoordBuffer);
        
        var chordVStart = (Math.floor(i/dimension))*texturePartition;
        var chordVEnd = (Math.floor((i+dimension)/dimension))*texturePartition;
        var chordUStart = 1-(((i%dimension) *texturePartition) + texturePartition);
        var chordUEnd = 1-(((i%dimension) *texturePartition));
        
        var textureCoords = 
        [
          // Front face
          chordVStart, chordUStart,
          chordVEnd, chordUStart,
          chordVEnd, chordUEnd,
          chordVStart, chordUEnd,

          // Back face
          0.0, 0.0,
          0.0, 0.0,
          0.0, 0.0,
          0.0, 0.0,


          // Top face
          0.0, 0.0,
          0.0, 0.0,
          0.0, 0.0,
          0.0, 0.0,

          // Bottom face
          0.0, 0.0,
          0.0, 0.0,
          0.0, 0.0,
          0.0, 0.0,

          // Right face
          0.0, 0.0,
          0.0, 0.0,
          0.0, 0.0,
          0.0, 0.0,

          // Left face
          0.0, 0.0,
          0.0, 0.0,
          0.0, 0.0,
          0.0, 0.0,
        ];
        
    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureCoords), gl.STATIC_DRAW);
    cubeVertexTextureCoordBuffer.itemSize = 2;
    cubeVertexTextureCoordBuffer.numItems = 24;
    cubeVertexTextureCoordBuffers.push(cubeVertexTextureCoordBuffer);
    }

    cubeVertexIndexBuffer = gl.createBuffer();
    gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, cubeVertexIndexBuffer);
    var cubeVertexIndices = [
        0, 1, 2,      0, 2, 3,    // Front face
        4, 5, 6,      4, 6, 7,    // Back face
        8, 9, 10,     8, 10, 11,  // Top face
        12, 13, 14,   12, 14, 15, // Bottom face
        16, 17, 18,   16, 18, 19, // Right face
        20, 21, 22,   20, 22, 23  // Left face
    ];
    gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(cubeVertexIndices), gl.STATIC_DRAW);
    cubeVertexIndexBuffer.itemSize = 1;
    cubeVertexIndexBuffer.numItems = 36;
}